<?php

namespace App\Http\Controllers;

use ElemeOpenApi\Config\Config;
use ElemeOpenApi\OAuth\OAuthClient;
use Illuminate\Foundation\Auth\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Session;

class AuthController extends Controller
{

    private $app_key;
    private $app_secret;
    private $callback_url;

    public function __construct()
    {
        $this->app_key = env('PLATFORM_KEY');
        $this->app_secret = env('PLATFORM_SECRET');
        $this->callback_url = env('CALLBACK_URL');
    }


    public function callback_get_auth(Request $request){
        $data = $request->all();
        switch($data['type'] == 'Elm'){
            case 'Elm':
                if(!$data['code']){
                    throw_error('code获取失败');
                }
                $config = new Config($this->app_key, $this->app_secret, true);
                $client = new OAuthClient($config);
                $token_data= $client->get_token_by_code($data['code'], $this->callback_url);
                if(!$token_data){
                    throw_error('token获取失败');
                }
                Cache::put('token_'.$data['state'],json_encode($token_data),28800);
                echo '成功';
                break;
            case 'Et':

            case 'Tdd':
        }
    }

    public function get_auth(Request $request)
    {
        $type = $request->input('type');
        $state = $request->input('state');
        switch($type == "Elm"){
            case "Elm":
                $config = new Config($this->app_key, $this->app_secret, true);
                $client = new OAuthClient($config);
                $auth_url = $client->get_auth_url($state, 'all', urlencode($this->callback_url));
                header('location:' . $auth_url);exit();
                break;
            case "Et":


            case "Tdd":
        }

    }



}
